
/*!
    \copyright  Copyright (c) 2024 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    @{
        \ingroup    adkfs
        \brief      Implements the System Monitor (sysmon) interface used by the ADK Filesystem

        Filesystem providers can use this interface to monitor the System's state
*/
#include "adkfs_sysmon_if.h"

#include "phy_state.h"
#include <message.h>
#include <stdbool.h>

typedef struct {
    /*! Physical status of device */
    phyState phy;
} adkfs_sysmon_state_t;

typedef struct
{
    /*! Task to handle phy state changes */
    TaskData task_data;
    adkfs_sysmon_state_t state;
    bool is_initialized;
} adkfs_sysmon_t;

static void adkfs_PhyStateChangeHandler(Task task, MessageId id, Message message);

static adkfs_sysmon_t sysmon =
{
    .task_data = {.handler = adkfs_PhyStateChangeHandler},
    .state = {
        .phy = PHY_STATE_UNKNOWN
    },
    .is_initialized = FALSE,
};

static void adkfs_PhyStateChangeHandler(Task task, MessageId id, Message message)
{
    UNUSED(task);
    if(id == PHY_STATE_CHANGED_IND)
    {
        PHY_STATE_CHANGED_IND_T* msg = (PHY_STATE_CHANGED_IND_T *)message;
        sysmon.state.phy = msg->new_state;
    }
}

static bool adkfs_SysmonSystemIsIdle(void *context)
{
    UNUSED(context);
    return sysmon.state.phy == PHY_STATE_IN_CASE;
}

static const adkfs_sysmon_if_t sysmon_if = {
    .systemIsIdle = adkfs_SysmonSystemIsIdle,
};

void adkFs_SysmonInit(void)
{
    appPhyStateRegisterClient(&sysmon.task_data);
}

const adkfs_sysmon_if_t* AdkFs_GetSysmonIf(void)
{
    if (!sysmon.is_initialized)
    {
        adkFs_SysmonInit();
    }
    return &sysmon_if;
}

/*! @} */